package com.ruoyi.classai.controller;

import com.ruoyi.classai.domain.EwcChapterResource;
import com.ruoyi.classai.domain.dto.chapterresource.EwcChapterResourceAddDto;
import com.ruoyi.classai.domain.dto.chapterresource.EwcChapterResourceQueryDto;
import com.ruoyi.classai.service.IEwcChapterResourceService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 老师资源库Controller
 *
 * @author lcj
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/teacherResource")
public class EwcChapterResourceController extends BaseController {

    @Autowired
    private IEwcChapterResourceService ewcChapterResourceService;

    /**
     * 查询老师资源库列表
     */
    @RequiresPermissions("classai:teacherResource:list")
    @GetMapping("/list")
    public TableDataInfo list(EwcChapterResourceQueryDto dto) {
        startPage();
        List<EwcChapterResource> list = ewcChapterResourceService.selectChapterResourceList(dto);
        return getDataTable(list);
    }

    /**
     * 导出老师资源库列表
     */
    @RequiresPermissions("classai:teacherResource:export")
    @Log(title = "老师资源库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EwcChapterResourceQueryDto dto) {
        List<EwcChapterResource> list = ewcChapterResourceService.selectChapterResourceList(dto);
        ExcelUtil<EwcChapterResource> util = new ExcelUtil<>(EwcChapterResource.class);
        util.exportExcel(response, list, "老师资源库数据");
    }

    /**
     * 获取老师资源库详细信息
     */
    @RequiresPermissions("classai:teacherResource:query")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success("查询成功", ewcChapterResourceService.getChapterResourceById(id));
    }

    /**
     * 新增老师资源库
     */
    @RequiresPermissions("classai:teacherResource:add")
    @Log(title = "老师资源库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated EwcChapterResourceAddDto dto) {
        ewcChapterResourceService.insertChapterResource(dto);
        return AjaxResult.success("操作成功！");
    }

    /**
     * 修改老师资源库
     */
    @RequiresPermissions("classai:teacherResource:edit")
    @Log(title = "老师资源库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid EwcChapterResourceAddDto dto) {
        ewcChapterResourceService.updateChapterResource(dto);
        return AjaxResult.success("操作成功！");
    }

    /**
     * 删除老师资源库
     */
    @RequiresPermissions("classai:teacherResource:remove")
    @Log(title = "老师资源库", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = Arrays.asList(ids);
        ewcChapterResourceService.deleteChapterResourceByIds(list);
        return AjaxResult.success("操作成功！");
    }
}
